﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security;
using System.Globalization;

namespace DynamicGeometry 
{
    /// <summary>
    /// Interaction logic for HyperlinkButton.xaml
    /// </summary>
    public partial class HyperlinkButton : UserControl
    {
        #region dependency properties
        /// <summary>
        /// Dependency property for hyperlink text content.
        /// </summary>
        public static readonly DependencyProperty HyperlinkContentProperty;
        #endregion

        #region routed events
        /// <summary>
        /// Routed event for handling the "button" click.
        /// </summary>
        public static readonly RoutedEvent ClickEvent;
        #endregion

        #region ctor
        #region static
        /// <summary>
        /// Initializes the <see cref="HyperlinkButton"/> class.
        /// </summary>
        static HyperlinkButton()
        {
            FrameworkPropertyMetadata fpm = new FrameworkPropertyMetadata(string.Empty);
            HyperlinkContentProperty = DependencyProperty.Register(
                "HyperlinkContent",
                typeof(string),
                typeof(FrameworkElement),
                fpm);

            ClickEvent = EventManager.RegisterRoutedEvent(
                "Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(HyperlinkButton));
        }
        #endregion

        #region instance
        /// <summary>
        /// Initializes a new instance of the <see cref="HyperlinkButton"/> class.
        /// </summary>
        public HyperlinkButton()
        {
            InitializeComponent();
        }
        #endregion
        #endregion

        #region properties
        /// <summary>
        /// Gets or sets the content of the hyperlink.
        /// </summary>
        /// <value>The content of the hyperlink.</value>
        public string HyperlinkContent
        {
            get { return (string)GetValue(HyperlinkContentProperty); }
            set { SetValue(HyperlinkContentProperty, value); }
        }

        /// <summary>
        /// Occurs when [click].
        /// </summary>
        public event RoutedEventHandler Click
        {
            add
            {
                AddHandler(ClickEvent, value);
            }
            remove
            {
                RemoveHandler(ClickEvent, value);
            }
        }
        #endregion

        #region event handlers
        /// <summary>
        /// Handles the MouseEnter event of the hyperlinkContentText control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private void hyperlinkContentText_MouseEnter(object sender, MouseEventArgs e)
        {
            hyperlinkContentText.TextDecorations = TextDecorations.Underline;
            hyperlinkContentText.Cursor = Cursors.Hand;
        }

        /// <summary>
        /// Handles the MouseLeave event of the hyperlinkContentText control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private void hyperlinkContentText_MouseLeave(object sender, MouseEventArgs e)
        {
            hyperlinkContentText.TextDecorations = null;
            hyperlinkContentText.Cursor = Cursors.Arrow;
        }

        /// <summary>
        /// Handles the MouseLeftButtonDown event of the hyperlinkContentText control. Maps it to the click
        /// event of this control and suppresses it after raising Click.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void hyperlinkContentText_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            RaiseEvent(new RoutedEventArgs(ClickEvent, this));
        }
        #endregion
    }
}
